package com.wangxiaotao.Controller;
import com.wangxiaotao.Pojo.Goods;
import com.wangxiaotao.Pojo.User;
import com.wangxiaotao.Pojo.Order;
import com.wangxiaotao.Service.AdminService;
import com.wangxiaotao.Service.LoginService;
import com.wangxiaotao.WebSocket.WebSocket;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    WebSocket webSocket;
    /** 上传目录名*/
    private static final String uploadFolderName = "uploadFiles";

    /** 允许上传的扩展名*/
    private static final String [] extensionPermit = {"txt","jpg","xls","zip"};
    @RequestMapping(value = "delete_order.do",method = RequestMethod.POST)//删除订单
    public String delete(int order_id,HttpServletRequest request){
        adminService.deleteoder(order_id);
        List<Order>orderList=(List<Order>)request.getSession().getAttribute("orderList");
        for(int i=0;i<orderList.size();i++){
            Order user_order=orderList.get(i);
            if(order_id==user_order.getOrder_id()){
                orderList.remove(i);
            }
        }
        request.getSession().setAttribute("orderList",orderList);//更新
        request.setAttribute("orderList",orderList);
        return "admin";
    }
    @RequestMapping(value = "delete_goods.do",method = RequestMethod.POST)//删除food
    public String delete_goods(int goods_id,String goods_url,HttpServletRequest request){
        String path = request.getSession().getServletContext().getRealPath("/");//获取路径
        String realurl=path+goods_url;
        try{
            File serchfile=new File(realurl);//创建文件
            if(serchfile.exists()){
                serchfile.delete();//删除图片
            }else{
                System.out.println("文件不存在");
            }
        }catch (Exception e){}
        adminService.deletefood(goods_id);
        List<Goods>goodsList=(List<Goods>)request.getSession().getAttribute("goodsList");
        for(int i=0;i<goodsList.size();i++){
            Goods goods=goodsList.get(i);
            if(goods_id==goods.getGoods_id()){
                goodsList.remove(i);
            }
        }
        request.getSession().setAttribute("goodsList",goodsList);
        request.setAttribute("goodsList",goodsList);
        return "admin_food";
    }
    @RequestMapping(value = "update_oder.do",method = RequestMethod.POST)//更新订单
    public String update_oder(Order order, HttpServletRequest request){
        adminService.updataorder(order);
        int order_id=order.getOrder_id();
        List<Order>orderList=(List<Order>)request.getSession().getAttribute("orderList");
        List<Order>temp_list=new ArrayList<>();
        for(int i=0;i<orderList.size();i++){//修改当前更新的数据
            Order order1=orderList.get(i);
            if(order_id==order1.getOrder_id()){
                temp_list.add(order);
            }
            else{
                temp_list.add(order1);
            }
        }
        request.getSession().setAttribute("orderList",temp_list);//更新
        request.setAttribute("orderList",temp_list);
        return "admin";
    }
    @RequestMapping(value = "update_goods.do",method = RequestMethod.POST)
    public  String update_goods(Goods goods,HttpServletRequest request){
        adminService.updatagoods(goods);
        int goods_id=goods.getGoods_id();
        List<Goods>goodsList=(List<Goods>)request.getSession().getAttribute("goodsList");
        List<Goods>temp_list=new ArrayList<>();
        for(int i=0;i<goodsList.size();i++){
            Goods goods1=goodsList.get(i);
            if(goods_id==goods1.getGoods_id()){
                temp_list.add(goods);
            }else{
                temp_list.add(goods1);
            }
        }
        request.getSession().setAttribute("goodsList",temp_list);
        request.setAttribute("goodsList",temp_list);
        return "admin_food";
    }
    @RequestMapping(value = "setisok.do",method = RequestMethod.POST)//确认订单
    public String setisok(int order_id,HttpServletRequest request){
        adminService.setisok(order_id);
        List<Order>orderList=(List<Order>)request.getSession().getAttribute("orderList");
        List<Order>temp_list=new ArrayList<>();
        for(int i=0;i<orderList.size();i++){//修改isok
            Order user_order=orderList.get(i);
            if(order_id==user_order.getOrder_id()){
                user_order.setIsok(1);
                temp_list.add(user_order);
            }
            else{
                temp_list.add(user_order);
            }
        }
        request.getSession().setAttribute("orderList",temp_list);//更新
        request.setAttribute("orderList",temp_list);
        return "admin";
    }
    @RequestMapping(value = "isokorders.do",method = RequestMethod.POST)
    public String isokorders(HttpServletRequest request){
        List<Order>orderList=adminService.isokorders();
        request.getSession().setAttribute("orderList",orderList);
        request.setAttribute("orderList",orderList);
        return "admin";
    }
    @RequestMapping(value = "unisokorders.do",method = RequestMethod.POST)
    public String unisokorders(HttpServletRequest request){
        List<Order>orderList=adminService.unisokorders();
        request.getSession().setAttribute("orderList",orderList);
        request.setAttribute("orderList",orderList);
        return "admin";
    }
    @RequestMapping(value = "allorders.do",method = RequestMethod.POST)
    public String allorders(HttpServletRequest request){
        List<Order>orderList=adminService.allorders();
        request.getSession().setAttribute("orderList",orderList);
        request.setAttribute("orderList",orderList);
        return "admin";
    }
    @RequestMapping(value = "goodshow.do",method = RequestMethod.POST)
    public String goodshow(int num,HttpServletRequest request){
        if(num==0){
            List<Goods> goodsList=adminService.getallgoods();
            request.getSession().setAttribute("goodsList",goodsList);
        }else if(num==1){
            List<Goods> goodsList=adminService.getmeishigoods();//美食
            request.getSession().setAttribute("goodsList",goodsList);
        }else if(num==2){
            List<Goods> goodsList=adminService.getyinpingoods();//饮品
            request.getSession().setAttribute("goodsList",goodsList);
        }else if(num==3){
            List<Goods> goodsList=adminService.getshuiguogoods();//水果
            request.getSession().setAttribute("goodsList",goodsList);
        }
        return "admin_food";
    }
    //操作账户
    @RequestMapping(value = "userset.do",method = RequestMethod.POST)//获取所有用户
    public String userset(HttpServletRequest request){
        List<User>userList=adminService.getalluser();
        request.getSession().setAttribute("userList",userList);
        request.setAttribute("userList",userList);
        return "admin_user";
    }
    @RequestMapping(value = "update_user.do",method = RequestMethod.POST)//更新用户
    public String update_user(User user,HttpServletRequest request){
        adminService.updatauser(user);
        int user_id=user.getUser_id();
        List<User>userList=(List<User>)request.getSession().getAttribute("userList");
        List<User>temp_list=new ArrayList<>();
        for(int i=0;i<userList.size();i++){
            User user1=userList.get(i);
            if(user_id==user1.getUser_id()){
                temp_list.add(user);
            }else{
                temp_list.add(user1);
            }
        }
        request.getSession().setAttribute("userList",temp_list);
        request.setAttribute("userList",temp_list);
        return "admin_user";
    }
    @RequestMapping(value = "delete_user.do",method = RequestMethod.POST)
    public String delete_user(int user_id,HttpServletRequest request){
        adminService.deleteuser(user_id);
        List<User>userList=(List<User>)request.getSession().getAttribute("userList");
        for(int i=0;i<userList.size();i++){
            User user=userList.get(i);
            if(user_id==user.getUser_id()){
                userList.remove(i);
            }
        }
        request.getSession().setAttribute("userList",userList);
        request.setAttribute("userList",userList);
        return "admin_user";
    }
    @RequestMapping(value = "upimg.do",method = RequestMethod.POST)//文件上传和goods设置
    public void upimg(@RequestParam("goods_url") MultipartFile file, String goods_name,Double goods_price,int goods_type,HttpServletRequest request) {
        String path = request.getSession().getServletContext().getRealPath("/images/upload/");
        String fileFileName=file.getOriginalFilename();//获取文件名称
        String endName=fileFileName.substring(fileFileName.lastIndexOf(".") + 1);//获取文件名后缀
        String startName=fileFileName.substring(0,fileFileName.indexOf("."));//获取文件名后缀之前的名称
        if(startName.length()>10){//名称长度大于10
            startName = startName.substring(0, 9);//取前10
        }
        String tempFileName=path+startName+"."+endName;//合并文件路径和名称
        File serchfile=new File(tempFileName);//创建文件
        String randomstring=null;//产生随机字符串
        String newname=startName+"."+endName;//拼接新名
        while (serchfile.exists()){//检测新建文件名是否重名 重名就随机产生名字
            randomstring= RandomStringUtils.randomAlphanumeric(10);//产生随机字符串
            newname=randomstring+'.'+endName;//拼接新名
            tempFileName=path+newname;//合并文件路径和名称
            serchfile=new File(tempFileName);//创建文件
        }
        try {
            //创建File对象，传入目标路径参数和文件名称
            File dir = new File(path,newname);
            file.transferTo(dir);//将上传的文件复制到制定目录upload下
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("异常了");
            return;
        }
        Goods goods=adminService.getmaxgoodid();
        int goods_id=goods.getGoods_id();
        goods_id++;
        Goods goods1=new Goods();
        goods1.setGoods_id(goods_id);
        goods1.setGoods_name(goods_name);
        goods1.setGoods_price(goods_price);
        goods1.setGoods_url("images/upload/"+newname);
        goods1.setGoods_type(goods_type);
        adminService.addgoods(goods1);
    }
}
